package com.matrix.gallery.ui.wallpaper.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.asLiveData
import com.matrix.basecore.net.BaseData
import com.matrix.basecore.net.WallpaperResultData
import com.matrix.gallery.net.GalleryResult
import com.matrix.gallery.respository.WallpaperResp
import com.matrix.gallery.ui.wallpaper.bean.PhoneComputerTypeBean
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flow

class PhoneComputerVM : ViewModel() {

    /**
     * 手机类别
     */
    fun phoneWallpaper(): LiveData<GalleryResult<out List<BaseData>>> {
        return flow {
            val result: WallpaperResultData<PhoneComputerTypeBean> = WallpaperResp.phoneWallpaper()
            if (result.code == 0) {
                var galleryResult = GalleryResult(GalleryResult.SUCCESS, result.res!!.category)
                emit(galleryResult)
            } else {
                var galleryResult = GalleryResult<List<BaseData>>(GalleryResult.FAIL, result.msg)
                emit(galleryResult)
            }
        }.catch { ex ->
            var galleryResult = GalleryResult<List<BaseData>>(GalleryResult.FAIL, ex.toString())
            emit(galleryResult)
        }.asLiveData()
    }

    /**
     * 电脑类别
     */
    fun computerWallpaper(): LiveData<GalleryResult<out List<BaseData>>> {
        return flow {
            val result: WallpaperResultData<PhoneComputerTypeBean> =
                WallpaperResp.computerWallpaper()
            if (result.code == 0) {
                var galleryResult = GalleryResult(GalleryResult.SUCCESS, result.res!!.category)
                emit(galleryResult)
            } else {
                var galleryResult = GalleryResult<List<BaseData>>(GalleryResult.FAIL, result.msg)
                emit(galleryResult)
                //error
            }
        }.catch { ex ->
            var galleryResult = GalleryResult<List<BaseData>>(GalleryResult.FAIL, ex.toString())
            emit(galleryResult)
        }.asLiveData()
    }

    /**
     * all 壁纸
     */
    fun getAllWallpaper(id: String, skip: Int): LiveData<GalleryResult<out List<BaseData>>> {
        return flow {
            val result = WallpaperResp.getAllWallpaper(id, 12, skip, false, 0, "new")
            if (result.code == 0) {
                var galleryResult = GalleryResult(GalleryResult.SUCCESS, result.res!!.vertical)
                emit(galleryResult)
            } else {
                var galleryResult = GalleryResult<List<BaseData>>(GalleryResult.FAIL, result.msg)
                emit(galleryResult)
                //error
            }
        }.catch { ex ->
            var galleryResult = GalleryResult<List<BaseData>>(GalleryResult.FAIL, ex.toString())
            emit(galleryResult)
        }.asLiveData()
    }

    /**
     * all 壁纸
     */
    fun getComputerWallpaper(
        id: String,
        skip: Int
    ):  LiveData<GalleryResult<out List<BaseData>>> {
        return flow {
            val result = WallpaperResp.getComputerWallpaper(id, 12, skip, false, 0, "hot")
            if (result.code == 0) {
                var galleryResult = GalleryResult(GalleryResult.SUCCESS, result.res!!.wallpaper)
                emit(galleryResult)
            } else {
                var galleryResult = GalleryResult<List<BaseData>>(GalleryResult.FAIL, result.msg)
                emit(galleryResult)
                //error
            }
        }.catch { ex ->
            var galleryResult = GalleryResult<List<BaseData>>(GalleryResult.FAIL, ex.toString())
            emit(galleryResult)
        }.asLiveData()
    }

}